package com;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.naming.NamingException;

import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.domain.Chatter;

@Service
public class DisplayChatters {
    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Transactional
    public List<Chatter> getUsers() throws ClassNotFoundException, IOException, SQLException, NamingException {
        List<Chatter> list = new ArrayList<Chatter>();
        String query = "SELECT * FROM users";

        ParameterizedRowMapper<Chatter> mapper = new ParameterizedRowMapper<Chatter>() {
            public Chatter mapRow(ResultSet rs, int rowNum) throws SQLException {
                Chatter chatter = new Chatter(rs.getString("nickname"));
                return chatter;
            }
        };
        list = (List<Chatter>) namedParameterJdbcTemplate.query(query, new HashMap(), mapper);
        return list;
    }

}
